﻿<!DOCTYPE html>
<html>
<head>
  <title>Pipe Tree</title>
  <!-- Copyright 1998-2021 by Northwoods Software Corporation. -->
  <meta name="description" content="A tree structure that does not use links." />
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <script src="../release/go.js"></script>
  <script src="../assets/js/goSamples.js"></script>  <!-- this is only for the GoJS Samples framework -->
  <script id="code">
    function init() {
      if (window.goSamples) goSamples();  // init for these samples -- you don't need to call this
      var $ = go.GraphObject.make;  // for conciseness in defining templates

      myDiagram =
        $(go.Diagram, "myDiagramDiv",  // Diagram refers to its DIV HTML element by id
          {
            isReadOnly: true,
            // define a TreeLayout where alternating layers of nodes grow in different directions
            // child and parent nodes have no space between them.
            layout:
              $(go.TreeLayout,
                {
                  treeStyle: go.TreeLayout.StyleAlternating,
                  angle: 90,
                  layerSpacing: 0,
                  alternateAngle: 0,
                  alternateLayerSpacing: 0
                })
          });

      // the node template
      // the shape will be resized appropriately when the model is set up
      myDiagram.nodeTemplate =
        $(go.Node, "Auto",
          $(go.Shape, "Rectangle",
            { name: "SHAPE", width: 30, height: 30 },
            new go.Binding("fill", "color"),
            new go.Binding("stroke", "color")),
          $(go.TextBlock, { name: "TEXTBLOCK", margin: 5 },
            new go.Binding("text", "flow", function(flow) { return getText(flow); }))
        );

      // the Links should have no graphical representation
      myDiagram.linkTemplate =
        $(go.Link);

      myDiagram.model = new go.TreeModel([
        { key: 1, flow: 92, color: "#808080" },
        { key: 2, "parent": 1, flow: 47, color: "#808080" },
        { key: 3, "parent": 1, flow: 45, color: "#808080" },
        { key: 4, "parent": 2, flow: 15, color: "#808080" },
        { key: 5, "parent": 2, flow: 17, color: "#808080" },
        { key: 6, "parent": 2, flow: 15, color: "#808080" },
        { key: 7, "parent": 5, flow: 8, color: "#FFFF00" },
        { key: 8, "parent": 5, flow: 9, color: "#FF0000" },
        { key: 9, "parent": 6, flow: 5, color: "#808080" },
        { key: 10, "parent": 6, flow: 5, color: "#808080" },
        { key: 11, "parent": 6, flow: 5, color: "#808080" }
      ]);

      myDiagram.delayInitialization(updatePipes);
    }

    // return the text for the TextBlock, using the current number to determine its name
    function getText(flow) {
      if (flow < 10) return "SubLateral -- Current: " + flow + " gpm";
      if (flow < 25) return "Lateral -- Current: " + flow + " gpm";
      if (flow < 50) return "SubMain -- Current: " + flow + " gpm";
      return "Main -- Max: 100 gpm  Current: " + flow + " gpm";
    }

    // give all shapes the appropriate dimensions and text color, size, and orientation.
    function updatePipes() {
      var updated = 1;  // when this is 0, no more nodes are in need of updating
      while (updated !== 0) {
        // have layout determine node positions first
        myDiagram.layoutDiagram();
        updated = 0;
        var nodes = myDiagram.nodes.iterator;
        while (nodes.next()) {
          var node = nodes.value;
          var shape = node.findObject("SHAPE");
          if (!areChildrenUpdated(node) || !(shape.width === shape.height)) continue;
          // update the node if all of its children have been updated and it has not
          // this allows its size to be determined based on its childrens' positions once they have been updated and repositioned
          else updated++;
          // depending on the lightness of the node's color, make the text black or white
          var colorBrightness = parseInt(shape.fill.substring(1, 3), 16) +
            parseInt(shape.fill.substring(3, 5), 16) +
            parseInt(shape.fill.substring(5, 7), 16);
          if (colorBrightness <= 384) { node.findObject("TEXTBLOCK").stroke = "white"; }
          var horiz;
          var linkIn = node.findTreeParentLink();
          if (linkIn === null) horiz = true;
          // the root node grows horizontally from the left, as do any nodes with links entering their left side
          else horiz = linkIn.toSpot.x === 0;
          var long = 70;
          // the length of the longer side of the shape
          if (node.findTreeChildrenLinks().count === 0) long = 170;
          var short = 20;
          // the length of the shorter side of the shape
          var flow = node.data.flow;
          // size of the shape depends on the node's "current"
          if (flow > 20) { short = 50; long += 30; }
          if (flow > 50) short = 100;
          // font size also depends on current
          node.findObject("TEXTBLOCK").font = Math.floor(10 + flow / 11) + "px sans-serif";
          var chl = node.findTreeChildrenNodes();
          if (horiz) {
            var min = node.location.x;
            var max = min;
            while (chl.next()) {
              if (min === max) if (chl.value.location.x < min) min = chl.value.location.x;
              if (chl.value.location.x > max) max = chl.value.location.x;
            }
            long += max - min;
            // make sure the shape is large enough to reach all children
            if (long < 160) long = 160;
            // a minimum shape size
            shape.height = short;
            shape.width = long;
            // the horizontal side is longer
            // set the shape's size
          } else {
            var min = node.location.y;
            var max = min;
            while (chl.next()) {
              if (min === max) if (chl.value.location.y < min) min = chl.value.location.y;
              if (chl.value.location.y > max) max = chl.value.location.y;
            }
            long += max - min;
            if (long < 160) long = 160;
            shape.height = long;
            shape.width = short;
            // the longer size is the vertical one in this case
            node.findObject("TEXTBLOCK").angle = 90;
            // rotate the TextBlock if the shape is longer vertically
          }
        }
      }
    }

    // check if the children of this node have all had their sizes changed from the initial one
    // if they have been updated, their widths and heights cannot be equal
    function areChildrenUpdated(node) {
      return node.findTreeChildrenNodes().all(function(child) {
        return (child.findObject("SHAPE").width !== child.findObject("SHAPE").height);
      });
    }
  </script>
</head>
<body onload="init()">
<div id="sample">
  <div id="myDiagramDiv" style="width:100%;height:600px;border:1px solid black;"></div>
  <p>
  This diagram does not display <a>Link</a>s.
  Instead the <a>TreeLayout.layerSpacing</a> is set to 0,
  so that each node and its children have no space between them.
  </p>
  <p>
  The <a>TreeLayout.treeStyle</a> is set to StyleAlternating,
  so that alternating layers of the tree grow in each of two directions.
  Each node's <a>TextBlock</a> is angled according to the direction
  of the layer of the tree that it is in, and the <a>Shape</a>'s
  size is set according to direction and the position of the node's children.
  </p>
</div>
</body>
</html>